/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ReportForm.java
 *
 * Created on Jan 10, 2011, 12:38:23 PM
 */

package Reports;

import java.io.File;
import java.net.URL;


/**
 *
 * @author leng
 */
public class ReportForm extends javax.swing.JPanel {

    /** Creates new form ReportForm */
    public ReportForm() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane = new javax.swing.JTextPane();

        jButton1.setText("jButton1");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(jTextPane);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jButton1)
                .addContainerGap(491, Short.MAX_VALUE))
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(58, 58, 58)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 233, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(283, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(19, 19, 19)
                .add(jButton1)
                .addContainerGap(371, Short.MAX_VALUE))
            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .add(93, 93, 93)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 124, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(196, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        refreshReport();
    }//GEN-LAST:event_jButton1ActionPerformed


    /**regenerate and reload all report.*/
    public void refreshReport(){

        // ##1## prepare folder for report output
        Reports.GenHTMLReport.PrepareFolder prefolder = new Reports.GenHTMLReport.PrepareFolder();
        String strDirectory = "reports";
        int flag = prefolder.check(strDirectory, env.CONSTANTS.SIMPLEPATH_UNDER_CURRENT_WORKING_FOLDER );
        if(flag<0){
            System.out.println("error 1101101335:: fail to prepare the directory: " + strDirectory + " !");
            return;
        }

        
        // ##2## get current working path
        File f = new File(".");
        String abspath = f.getAbsolutePath();
        abspath = abspath.substring(0, abspath.length()-2);
        System.out.println("[report system] output directory is " + abspath );


        // ##3## gen index.html
        Reports.GenHTMLReport.genIndexHTML genindex = new Reports.GenHTMLReport.genIndexHTML();
        String str_indexhtml = abspath+File.separator + strDirectory + File.separator;
        int success = genindex.output(str_indexhtml);
        if(success < 0){
            System.out.println("[report system] fail to output " 
                    + str_indexhtml
                    + "index.html");
            return;
        }else
            System.out.println("[report system] outputed "
                    + str_indexhtml
                    + "index.html successfully.");


        // ##4## show index.html in the inner web browser in eHOST
        this.jTextPane.setEditable(false);
        try {
            File indexhtml = new File(str_indexhtml +"index.html");
            URL url = indexhtml.toURI().toURL();
            jTextPane.setPage( url );
        } catch (Exception ex) {
            System.out.println("error 1101101459::fail to show index.html on screen:\n"
                    + ex.getMessage());
        }

        // get all annotators
        Reports.Matcher.AnnotatorRegister areg = new Reports.Matcher.AnnotatorRegister();
        int count = areg.register();
        System.out.println("[report system] total " + count + " annotators registered in report system now.");
        if (count < 1){
            return;
        }

        // check for same span annotaions
        // rules can be find in class ""

        


        // ##5## generate "class matcher.html" file:
        Reports.GenHTMLReport.genClassMatcher gencm = new Reports.GenHTMLReport.genClassMatcher();
        String str_gencm = abspath+File.separator + strDirectory + File.separator;
        int success_cm = gencm.output(str_gencm);
        if(success_cm < 0){
            System.out.println("[report system] fail to output "
                    + str_gencm                    
                    + "Class matcher.html");
            return;
        }else
            System.out.println("[report system] outputed "
                    + str_gencm
                    + "Class matcher.html successfully.");

        

        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane jTextPane;
    // End of variables declaration//GEN-END:variables

}
